// Code copyright S. Stancliff. See gotemp.c
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <errno.h>
#include <string.h>

FILE *fp;

int gotemp_open(){
	char port[32] = "/dev/ldusb0";
	fp = fopen(port,"rb");

	if(fp==NULL){
		printf("failed to open %s\n",port);
		return(-255.);
	}
	return 0;
}


double gotemp_read(){
	double rval;
	uint8_t i[8];
	
	fread(i,sizeof(uint8_t),8,fp);
	
//	{
//	register int j;
	
//	for(j=0;j<8;j++){
//		printf("i[%d] = %d\n",j,i[j]);
//	}
//	}
	
	uint16_t k;
	uint8_t  *m;
	
	m = (uint8_t *)&k;
	
	*m = i[4];
	*(m+1) = i[3];
	
	rval = (double)k/128;
//	printf("temp = %.1f *C\n",(double)k/128);
//	printf("temp = %.1f *F\n",(double)k/128*9/5+32);

	
	
//	fclose(fp);
	
	return(rval);
}

